package com.ruoyi.organization.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.CommunityScope;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.BizCommunity;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.organization.mapper.BizCommunityMapper;
import com.ruoyi.organization.service.IBizCommunityService;

/**
 * 社区设置Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-06
 */
@Service
public class BizCommunityServiceImpl implements IBizCommunityService 
{
    @Autowired
    private BizCommunityMapper bizCommunityMapper;

    /**
     * 查询社区设置
     * 
     * @param communityId 社区设置主键
     * @return 社区设置
     */
    @Override
    public BizCommunity selectBizCommunityByCommunityId(Long communityId)
    {
        return bizCommunityMapper.selectBizCommunityByCommunityId(communityId);
    }

    /**
     * 查询社区设置列表
     * 
     * @param bizCommunity 社区设置
     * @return 社区设置
     */
    @Override
    @CommunityScope
    public List<BizCommunity> selectBizCommunityList(BizCommunity bizCommunity)
    {
        return bizCommunityMapper.selectBizCommunityList(bizCommunity);
    }

    /**
     * 新增社区设置
     * 
     * @param bizCommunity 社区设置
     * @return 结果
     */
    @Override
    public int insertBizCommunity(BizCommunity bizCommunity)
    {
        bizCommunity.setCreateTime(DateUtils.getNowDate());
        return bizCommunityMapper.insertBizCommunity(bizCommunity);
    }

    /**
     * 修改社区设置
     * 
     * @param bizCommunity 社区设置
     * @return 结果
     */
    @Override
    public int updateBizCommunity(BizCommunity bizCommunity)
    {
        bizCommunity.setUpdateTime(DateUtils.getNowDate());
        return bizCommunityMapper.updateBizCommunity(bizCommunity);
    }

    /**
     * 批量删除社区设置
     * 
     * @param communityIds 需要删除的社区设置主键
     * @return 结果
     */
    @Override
    public int deleteBizCommunityByCommunityIds(Long[] communityIds)
    {
        return bizCommunityMapper.deleteBizCommunityByCommunityIds(communityIds);
    }

    /**
     * 删除社区设置信息
     * 
     * @param communityId 社区设置主键
     * @return 结果
     */
    @Override
    public int deleteBizCommunityByCommunityId(Long communityId)
    {
        return bizCommunityMapper.deleteBizCommunityByCommunityId(communityId);
    }
    /**
     * 查询部门管理数据
     *
     * @param bizCommunity 部门信息
     * @return 部门信息集合
     */
    @Override
    public List<BizCommunity> selectDeptList(BizCommunity bizCommunity)
    {
        return bizCommunityMapper.selectBizCommunityList(bizCommunity);
    }
    /**
     * 查询社区树结构信息
     *
     * @param bizCommunity
     * @return
     */
    @Override
    public List<TreeSelect> selectCommunityTreeTreeList(BizCommunity bizCommunity) {
        List<BizCommunity> communities = this.selectDeptList(bizCommunity);
        return buildDeptTreeSelect(communities);
    }

    /**
     * 无权限
     *
     * @param bizCommunity
     * @return
     */
    @Override
    public List<BizCommunity> selectBizCommunityListFromNoScope(BizCommunity bizCommunity) {
        return bizCommunityMapper.selectBizCommunityListNoScope(bizCommunity);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param bizCommunities 部门列表
     * @return 下拉树结构列表
     */
    private List<TreeSelect> buildDeptTreeSelect(List<BizCommunity> bizCommunities)
    {
        List<BizCommunity> communitiesTrees = buildDeptTree(bizCommunities);
        return communitiesTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    private List<BizCommunity> buildDeptTree(List<BizCommunity> depts)
    {
        List<BizCommunity> returnList = new ArrayList<BizCommunity>();
        List<Long> tempList = depts.stream().map(BizCommunity::getCommunityId).collect(Collectors.toList());
        for (BizCommunity dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<BizCommunity> list, BizCommunity t)
    {
        // 得到子节点列表
        List<BizCommunity> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BizCommunity tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<BizCommunity> getChildList(List<BizCommunity> list, BizCommunity t)
    {
        List<BizCommunity> tlist = new ArrayList<BizCommunity>();
        Iterator<BizCommunity> it = list.iterator();
        while (it.hasNext())
        {
            BizCommunity n = (BizCommunity) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getCommunityId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BizCommunity> list, BizCommunity t)
    {
        return getChildList(list, t).size() > 0;
    }
}
